package com.sunny.ynhz.ui.lake;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.PatrolProblemAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.SeasureAppBean;
import com.sunny.ynhz.ui.PartolLakeDetailActivity;
import com.sunny.ynhz.ui.base.CustomLayFragment;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/5/21.
 */

public class PatrolBasinRiverProblemFragment extends CustomLayFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    private List<SeasureAppBean.DataBean.ContentBean> content = new ArrayList<>();
    private RecyclerView rv_patrol_river;
    private SwipeRefreshLayout swipeRefreshLayout_ly;
    private PatrolProblemAdapter patrolProblemAdapter;
    private int page = 1;

    public void initData() {
        hasLoaded = true;
        String type = getArguments().getString("type");
        HttpManager.getInstance().getbasinMsg(type, page, 10).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(getContext(), new ApiSubscriber.HttpOnResultListener<SeasureAppBean>() {
            @Override
            public void onNext(SeasureAppBean seasureAppBean) {
                if (page == 1) {
                    content.clear();
                    patrolProblemAdapter.notifyDataSetChanged();
                }
                swipeRefreshLayout_ly.setRefreshing(false);
                content.addAll(seasureAppBean.getData().getContent());
                if (content.size()<seasureAppBean.getData().getRecordTotal()) {
                    page++;
                    patrolProblemAdapter.setEnableLoadMore(true);
                } else {
                    patrolProblemAdapter.setEnableLoadMore(false);
                }
                patrolProblemAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCompleted() {
                swipeRefreshLayout_ly.setRefreshing(false);
                patrolProblemAdapter.loadMoreComplete();

            }

            @Override
            public void onstart() {

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                swipeRefreshLayout_ly.setRefreshing(false);
                patrolProblemAdapter.loadMoreComplete();
            }
        }));

    }

    @Override
    public void setUpView() {
        rv_patrol_river = mContentView.findViewById(R.id.rv_patrol_river);
        swipeRefreshLayout_ly = mContentView.findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout_ly.setOnRefreshListener(this);

        rv_patrol_river.setLayoutManager(new LinearLayoutManager(getContext()));
        patrolProblemAdapter = new PatrolProblemAdapter(content, getContext());
        rv_patrol_river.setAdapter(patrolProblemAdapter);
        patrolProblemAdapter.setOnLoadMoreListener(this);
        patrolProblemAdapter.setOnItemClickListener((adapter, view, position) -> {
            SeasureAppBean.DataBean.ContentBean contentBean = content.get(position);
            String patrolid = contentBean.getProblemid();
            Intent intent = new Intent(getContext(), PartolLakeDetailActivity.class);
            intent.putExtra("problemid", patrolid);
            startActivity(intent);
        });
    }

    @Override
    public int setLayoutId() {
        return R.layout.patrol_lake_fragment;
    }

    @Override
    public void setupData() {
        if (content.size() == 0) {
            swipeRefreshLayout_ly.setRefreshing(true);
            initData();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        initData();

    }

    @Override
    public void onLoadMoreRequested() {
        initData();

    }
}


